package com.dormitory_springboot.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.Claim;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.dormitory_springboot.bean.Admin;
import com.dormitory_springboot.bean.Homemaster;
import com.dormitory_springboot.bean.Student;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @auther 夏杨
 **/
@Component
public class JWTutil {
    static String key = "2a75c96d";
    static int outTime = 1;
//    @Resource
//    static StringRedisTemplate template;
//    public static boolean invalidateJwt(String token){
//        if(token == null) return false;
//        Algorithm algorithm = Algorithm.HMAC256(key);
//        JWTVerifier jwtVerifier = JWT.require(algorithm).build();
//        try{
//            DecodedJWT jwt = jwtVerifier.verify(token);
//            String id = jwt.getId();
//            return deleteToken(id,jwt.getExpiresAt());
//        }catch (Exception e){
//            return false;
//        }
//    }
//    private static boolean deleteToken(String uuid,Date time){
//        if(isInvalidToken(uuid)) return false;
//        Date now = new Date();
//        long expire = Math.max(time.getTime() - now.getTime(),0);
//        template.opsForValue().set(Const.JWT_BLACK_LIST + uuid,"",expire, TimeUnit.MICROSECONDS);
//        return true;
//    }
//    private static boolean isInvalidToken(String uuid){
//        return Boolean.TRUE.equals(template.hasKey(Const.JWT_BLACK_LIST+uuid));
//    }
    public static String creatJWTByAdimin(Admin admin){
        Algorithm algorithm = Algorithm.HMAC256(key);
        Date expireTime = expireTime();
        return JWT.create()
                .withJWTId(UUID.randomUUID().toString())
                .withClaim("rool","admin")
                .withClaim("id",admin.getId())
                .withClaim("password",admin.getPassword())
                .withExpiresAt(expireTime)
                .withIssuedAt(new Date())
                .sign(algorithm);
    }
    public static String creatJWTByHomemaster(Homemaster homemaster){
        Algorithm algorithm = Algorithm.HMAC256(key);
        Date expireTime = expireTime();
        return JWT.create()
                .withJWTId(UUID.randomUUID().toString())
                .withClaim("rool","homemaster")
                .withClaim("id",homemaster.getHmid())
                .withClaim("password",homemaster.getPassword())
                .withExpiresAt(expireTime)
                .withIssuedAt(new Date())
                .sign(algorithm);
    }
    public static String creatJWTByStudent(Student student){
        Algorithm algorithm = Algorithm.HMAC256(key);
        Date expireTime = expireTime();
        return JWT.create()
                .withJWTId(UUID.randomUUID().toString())
                .withClaim("rool","student")
                .withClaim("id",student.getSid())
                .withClaim("password",student.getPassword())
                .withExpiresAt(expireTime)
                .withIssuedAt(new Date())
                .sign(algorithm);
    }
    private static Date expireTime(){
        Calendar calendar = Calendar.getInstance();
        System.out.println();
        calendar.add(Calendar.HOUR,outTime*24);
        return calendar.getTime();
    }
    public static boolean resolveJwt(String token){
        if (token == null) return false;
        Algorithm algorithm = Algorithm.HMAC256(key);
        JWTVerifier jwtVerifier = JWT.require(algorithm).build();
        try {
            //当token被认为篡改时会报异常
            DecodedJWT verify = jwtVerifier.verify(token);
//            if(isInvalidToken(verify.getId())) return false;
            //获得过期时间
            Date expireAt = verify.getExpiresAt();
            Map<String, Claim> claims = verify.getClaims();
            //判断是否还有效
            return !(new Date().after(expireAt));
        }catch (Exception e){
            return false;
        }

    }
}
